namespace ETestIT.UI.Presentation
{
    using System.Web.UI;
    using Authentication;
    using Castle.MicroKernel.Registration;
    using Castle.Windsor;
    using SharpArch.Core.NHibernateValidator.CommonValidatorAdapter;
    using SharpArch.Core.PersistenceSupport;
    using SharpArch.Core.PersistenceSupport.NHibernate;
    using SharpArch.Data.NHibernate;
    using Storage;

    public class ComponentRegistrar
    {
        public static void AddComponentsTo(IWindsorContainer container)
        {
            AddGenericRepositoriesTo(container);
            AddCustomRepositoriesTo(container);
            AddCoreServicesTo(container);
            AddWebComponentsTo(container);

            container.AddComponent("validator",
                typeof(IValidator), typeof(Validator));
        }

        private static void AddWebComponentsTo(IWindsorContainer container)
        {
            container.AddComponent("sessionProvider", typeof(ISessionProvider), typeof(SessionProvider));
            container.AddComponent("authService", typeof(IAuthenticationService), typeof(AuthenticationServiceImpl));
        }

        private static void AddCoreServicesTo(IWindsorContainer container)
        {
            container.Register(
                AllTypes.Pick()
                .FromAssemblyNamed("ETestIT.Core")
                .WithService.FirstNonGenericCoreInterface("ETestIT.Core"));
        }

        private static void AddCustomRepositoriesTo(IWindsorContainer container)
        {
            container.Register(
                AllTypes.Pick()
                .FromAssemblyNamed("ETestIT.Data")
                .WithService.FirstNonGenericCoreInterface("ETestIT.Core"));
        }

        private static void AddGenericRepositoriesTo(IWindsorContainer container)
        {
            container.AddComponent("entityDuplicateChecker",
                typeof(IEntityDuplicateChecker), typeof(EntityDuplicateChecker));
            container.AddComponent("repositoryType",
                typeof(IRepository<>), typeof(Repository<>));
            container.AddComponent("nhibernateRepositoryType",
                typeof(INHibernateRepository<>), typeof(NHibernateRepository<>));
            container.AddComponent("repositoryWithTypedId",
                typeof(IRepositoryWithTypedId<,>), typeof(RepositoryWithTypedId<,>));
            container.AddComponent("nhibernateRepositoryWithTypedId",
                typeof(INHibernateRepositoryWithTypedId<,>), typeof(NHibernateRepositoryWithTypedId<,>));
        }
    }
}